import { GameRes } from "../../GameRes";
import { EventNames } from "../Data/EventNames";
import { ViewVideoNodeGameRunBase } from "./ViewVideoNodeGameRun.generated";

const { regClass, property } = Laya;

@regClass()
export class ViewVideoNodeGameRun extends ViewVideoNodeGameRunBase {
    public actPlayEnd: (isEnd: boolean) => void;
    /**是否视频播放结束 */
    private isVideoEnd: boolean = false;
    private tweenBoxIn: Laya.Tween;
    onAwake(): void {
        this.init();
        this.addEventListener();
    }

    onDestroy(): void {
        this.removeEventListener();
    }

    public showView(url: string, actPlayEnd: (isEnd: boolean) => void = undefined): void {
        Laya.SoundManager.stopMusic();
        this.visible = true;
        this.BoxView.left = this.width;
        this.BoxView.right = -this.width;
        this.actPlayEnd = actPlayEnd;
        this.playVideo(url);
        this.tweenBoxIn = Laya.Tween.to(this.BoxView, { left: 0, right: 0 }, 500, Laya.Ease.sineIn)
    }
    public hideView() {
        if (this.tweenBoxIn) {
            Laya.Tween.clear(this.tweenBoxIn);
        }
        this.visible = false;
    }

    private addEventListener(): void {
        this.btnClose.on(Laya.Event.CLICK, this, this.onClickViewVideoNodeCloseBtn);
        this.btnContinue.on(Laya.Event.CLICK, this, this.onClickContinueBtn);
        this.btnGiveUp.on(Laya.Event.CLICK, this, this.onClickGiveUpBtn);
    }

    private removeEventListener(): void {
        this.btnClose.off(Laya.Event.CLICK, this, this.onClickViewVideoNodeCloseBtn);
        this.btnContinue.off(Laya.Event.CLICK, this, this.onClickContinueBtn);
        this.btnGiveUp.off(Laya.Event.CLICK, this, this.onClickGiveUpBtn);
    }

    private init() {
        this.PopupViewVideoNodeTips.visible = false;
        this.videoNode.pos(this.width / 2, this.height / 2, true);
    }
    /**监听点击关闭视频页面按钮 */
    private onClickViewVideoNodeCloseBtn(): void {
        Laya.SoundManager.playSound(GameRes.clickAudio, 1);
        console.log("this.videoNode", this.videoNode);

        this.videoNode.pause(); //暂停视频
        if (this.isVideoEnd) {
            this.visible = false;
            console.log("看完视频了，可以领取奖励");
            return;
        }
        this.PopupViewVideoNodeTips.visible = true;
    }
    /**监听点击继续观看按钮 */
    private onClickContinueBtn(): void {
        Laya.SoundManager.playSound(GameRes.clickAudio, 1);
        this.videoNode.play(); //暂停视频
        this.PopupViewVideoNodeTips.visible = false;
    }
    /**监听点击放弃奖励按钮 */
    private onClickGiveUpBtn(): void {
        Laya.SoundManager.playSound(GameRes.clickAudio, 1);
        this.visible = false;
        this.PopupViewVideoNodeTips.visible = false;
        this.playBGM();
        this.actPlayEnd?.(false);
        this.actPlayEnd = undefined;
    }

    /**播放视频 */
    private playVideo(url: string): void {
        this.isVideoEnd = false;
        this.videoNode.source = url;
        console.log("视频添加监听");
        this.videoNode.videoTexture.on("ended", this, () => {
            this.isVideoEnd = true;
            this.playBGM();
            this.actPlayEnd?.(true);
            this.actPlayEnd = undefined;
        });
        console.log("视频添加监听");
        this.videoNode.videoTexture.on(Laya.Event.READY, () => {
            this.adaptiveVideo();
        });
        console.log("视频添加监听");
        this.videoNode.videoTexture.on("play", this, () => {
            console.log("视频开始播放play");
            console.log("视频开始播放play", this.videoNode);
        });
        console.log("加载视频并播放");
        this.videoNode.reload(); //播放视频
        this.videoNode.play();
    }

    private playBGM(): void { 
        Laya.stage.event(EventNames.PLAY_MUSIC_BGM)
    }

    /**视频做适配宽高 */
    private adaptiveVideo(): void {
        let videoWidth = this.videoNode.videoWidth;
        let videoHeight = this.videoNode.videoHeight;
        console.log("videoWidth=>", videoWidth);
        console.log("videoHeight=>", videoHeight);
        let rate: number = 1;
        if (videoWidth >= videoHeight) {
            rate = videoHeight / videoWidth;
            this.videoNode.width = this.width;
            this.videoNode.height = this.width * rate;
        } else {
            rate = videoWidth / videoHeight;
            this.videoNode.width = this.height * rate;
            this.videoNode.height = this.height;
        }
    }
}